/// 好友实体模型
/// 
/// 用于表示用户好友列表中的单个好友信息
class FriendEntity {
  /// 好友ID
  final int? friendId;
  
  /// 好友用户名
  final String? username;
  
  /// 好友头像URL
  final String? userAvatarUrl;
  
  /// 好友性别
  final String? userGender;
  
  /// 好友签名
  final String? userSignature;
  
  /// 好友状态
  final String? status;
  
  /// 好友分组ID
  final int? groupId;
  
  /// 好友标签
  final List<String>? userTags;
  
  /// 好友手机号（可能为null）
  final String? userPhone;
  
  /// 好友积分
  final int? userPoints;
  
  /// 好友等级
  final int? userLevel;

  FriendEntity({
    this.friendId,
    this.username,
    this.userAvatarUrl,
    this.userGender,
    this.userSignature,
    this.status,
    this.groupId,
    this.userTags,
    this.userPhone,
    this.userPoints,
    this.userLevel,
  });

  /// 从JSON创建好友实体
  factory FriendEntity.fromJson(Map<String, dynamic> json) {
    // ✅ 处理 status 字段：可能是 int 或 String
    String? statusValue;
    final statusField = json['status'];
    if (statusField != null) {
      if (statusField is int) {
        statusValue = statusField.toString();
      } else if (statusField is String) {
        statusValue = statusField;
      }
    }
    
    return FriendEntity(
      friendId: json['friend_id'] ?? json['friendId'],
      username: json['username'] as String?,
      userAvatarUrl: json['userAvatarUrl'] as String?,
      userGender: json['userGender'] as String?,
      userSignature: json['userSignature'] as String?,
      status: statusValue,
      groupId: json['group_id'] ?? json['groupId'],
      userTags: (json['userTags'] as List<dynamic>?)?.map((e) => e.toString()).toList(),
      userPhone: json['userPhone'] as String?,
      userPoints: json['userPoints'] as int?,
      userLevel: json['userLevel'] as int?,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'friend_id': friendId,
      'username': username,
      'userAvatarUrl': userAvatarUrl,
      'userGender': userGender,
      'userSignature': userSignature,
      'status': status,
      'group_id': groupId,
      'userTags': userTags,
      'userPhone': userPhone,
      'userPoints': userPoints,
      'userLevel': userLevel,
    };
  }

  /// 获取显示名称
  String get displayName => username ?? '未知用户';
  
  /// 获取头像URL（带默认值）
  String get avatarUrl => userAvatarUrl ?? '';
  
  /// 获取性别文本
  String get genderText {
    switch (userGender) {
      case '男':
        return '男';
      case '女':
        return '女';
      default:
        return '未知';
    }
  }

  @override
  String toString() {
    return 'FriendEntity(friendId: $friendId, username: $username)';
  }
}

